﻿using System.Diagnostics;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;

namespace Pap.Web.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class PapRoutingHandler : IRouteHandler
    {
        #region IRouteHandler Members

        /// <summary>
        /// Gets the HTTP handler.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <returns></returns>
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            string view = requestContext.RouteData.Values["View"].ToString();
            string module = requestContext.RouteData.Values["Module"].ToString();
            //-------------------------------------------------------------------
            var page = CreatePage(view, module);
            //-------------------------------------------------------------------
            page.set_Module(module);
            page.set_View(view);
            return page;
        }

        /// <summary>
        /// Creates the page.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="module">The module.</param>
        /// <returns></returns>
        [DebuggerStepThrough]
        private static Page CreatePage(string view, string module)
        {
            return BuildManager.CreateInstanceFromVirtualPath(
                            string.Format(UrlTemplates.ViewsUrlTemplate, module, view), typeof(Page)) as Page;
        }
        #endregion
    }
}
